/*
 * @Description: 
 * @Author: 于子续
 * @Date: 2024-09-03 15:54:17
 * @LastEditors: 于子续
 * @LastEditTime: 2024-09-03 15:56:18
 */
package day07;


import java.util.Scanner;
import java.util.Scanner;


public class Test9_DayOfYea {


    public class Test8_DayOfYear {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            System.out.print("请输入年份 月份 日期（如：2023 04 27）：");

            // 读取输入的年份、月份和日期
            int year = scanner.nextInt();
            scanner.next(); // 读取空格
            int month = scanner.nextInt();
            scanner.next(); // 读取空格
            int day = scanner.nextInt();

            // 检查输入是否有效
            if (month < 1 || month > 12 || day < 1 || (month == 2 && day > (isLeapYear(year) ? 29 : 28)) ||
                    (month == 4 || month == 6 || month == 9 || month == 11 && day > 30)) {
                System.out.println("输入的日期有误");
                return;
            }

            // 计算这一天是该年的第几天
            int dayOfYear = 0;
            for (int i = 1; i < month; i++) {
                dayOfYear += getDaysInMonth(year, i);
            }
            dayOfYear += day;

            System.out.println(dayOfYear);
        }

        // 方法：判断是否是闰年
        public static boolean isLeapYear(int year) {
            return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
        }

        // 方法：获取某年某月的天数
        public static int getDaysInMonth(int year, int month) {
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    return 31;
                case 4:
                case 6:
                case 9:
                case 11:
                    return 30;
                case 2:
                    return isLeapYear(year) ? 29 : 28;
                default:
                    throw new IllegalArgumentException("Invalid month: " + month);
            }
        }
    }
}
